package com.dogdays;

import org.jooq.util.DefaultGeneratorStrategy;
import org.jooq.util.GenerationTool;
import org.jooq.util.JavaGenerator;
import org.jooq.util.jaxb.*;
import org.jooq.util.mysql.MySQLDatabase;

/**
 * Created by jensklingsporn on 10.12.16.
 */
public class MysqlGeneratedTool {

    private static final String TARGET_FOLDER = System.getProperty("user.dir") + "/account-service/src/main/java";

    public static void main(String[] args) throws Exception {
        Configuration config = createGeneratorConfig(JavaGenerator.class.getName(), "com.dogdays.account.generated");
        GenerationTool.generate(config);
    }

    static Configuration createGeneratorConfig(String generatorName, String packageName){

        /*
         * We're using mysql to generate our files
         */
        Configuration configuration = new Configuration();
        Database databaseConfig = new Database();
        databaseConfig.setName(MySQLDatabase.class.getName());
        databaseConfig.setInputSchema("dogdays");
        databaseConfig.setOutputSchema("dogdays");

        Target targetConfig = new Target();
        targetConfig.setPackageName(packageName);
        targetConfig.setDirectory(TARGET_FOLDER);

        Generate generateConfig = new Generate();
        /*
         * When you set the interfaces-flag to true (recommended), the fromJson and toJson methods
         * are added as default-methods to the interface (so also jooq.Records will benefit)
         */
        generateConfig.setInterfaces(false);
        generateConfig.setPojos(true);
        generateConfig.setFluentSetters(false);
        generateConfig.setDaos(false);

        /*
         * We need to do a small hack to let jOOQ's DAOImpl implement our interface. That's why
         * we need a custom Strategy.
         */
        Strategy strategy = new Strategy();
        strategy.setName(DefaultGeneratorStrategy.class.getName());

        Generator generatorConfig = new Generator();
        generatorConfig.setName(generatorName);
        generatorConfig.setDatabase(databaseConfig);
        generatorConfig.setTarget(targetConfig);
        generatorConfig.setGenerate(generateConfig);
        generatorConfig.setStrategy(strategy);
        configuration.setGenerator(generatorConfig);

        Jdbc jdbcConfig = new Jdbc();
        jdbcConfig.setDriver("com.mysql.cj.jdbc.Driver");
        jdbcConfig.setUrl("jdbc:mysql://localhost:3306/dogdays?characterEncoding=UTF-8&useSSL=false");
        jdbcConfig.setUser("root");
        jdbcConfig.setPassword("wuban19901011");
        configuration.setJdbc(jdbcConfig);

        return configuration;
    }

}
